/**
 * Created by DELL on 2017/9/4.
 */
import axios from 'axios';
import Qs from 'qs'
import config from './global';

let util = {

};
function getCookie(name){
  var arr=document.cookie.split('; ');
  var i=0;
  for(i=0;i<arr.length;i++)
  {
    var arr2=arr[i].split('=');

    if(arr2[0]==name)
    {
      var getC = decodeURIComponent(arr2[1]);
      return getC;
    }
  }

  return '';
}
//var token = getCookie('token');
//console.log(token)
var oToken = getCookie('token');

const ajaxUrl = config.developmentUrl;
util.ajax = axios.create({
  baseURL: ajaxUrl.host,
  transformRequest: (data) => {
    return Qs.stringify(data)
  },
  headers: {
    // 'Authorization':auth,
    'Content-Type': 'application/x-www-form-urlencoded; charset=utf-8'
  },
  timeout: 5000
});
util.ajax.interceptors.request.use(
  config => {
    if (getCookie('token') !== oToken) {  // 判断是否存在token，如果存在的话，则每个http header都加上token
      oToken = getCookie('token');
      config.headers.Token =oToken;
    }else {
      config.headers.Token =oToken;
    }
    return config;
  },
  err => {
    return Promise.reject(err);
  });

export default util;
